package vn.kiemviec.app.fragments;

import java.util.ArrayList;

import vn.kiemviec.app.MainActivity;
import vn.kiemviec.app.R;
import vn.kiemviec.app.data.IndustryEntry;
import vn.kiemviec.app.task.GetIndustryTask;
import vn.kiemviec.app.task.GetIndustryTask.OnGetIndustryListener;
import vn.kiemviec.app.utils.LogTools;
import vn.kiemviec.app.viewadapter.IndustryAdapter;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class HomeFragment extends NavFragment {

	private Button btnAdvanceSearch;
	private ListView lsvIndustry;
	private EditText edtSearch;
	
	private ArrayList<IndustryEntry> listIndustry = new ArrayList<IndustryEntry>();
	private IndustryAdapter adapter;
	
	@Override
	public int OnGetLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.frag_home;
	}

	private void loadIndustry(){
		
		GetIndustryTask task = new GetIndustryTask();
		task.setOnIndustryListener(new OnGetIndustryListener() {
			
			@Override
			public void onSuccessfull(ArrayList<IndustryEntry> listEntry) {
				// TODO Auto-generated method stub
				listIndustry	= listEntry;
				adapter 		= new IndustryAdapter(getActivity(), listIndustry);
				lsvIndustry.setAdapter(adapter);
			}
			
			@Override
			public void onError(int error) {
				// TODO Auto-generated method stub
				
			}
		});
		task.execute();
	}
	
	@Override
	public void onSetUpView() {
		// TODO Auto-generated method stub
		btnAdvanceSearch 	= findView(R.id.btnAdvance);
		lsvIndustry 		= findView(R.id.lsvIndustry);
		edtSearch			= findView(R.id.edtSearch);
		
		loadIndustry();
	}
	
	@Override
	public void onSetUpEventHandler() {
		// TODO Auto-generated method stub
		
		btnAdvanceSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				pushFragment(new AdvanceSearchFragment());
				MainActivity parrent = (MainActivity)getActivity();
				if(parrent != null){
					parrent.showTab(1);
				}
			}
		});
		
		lsvIndustry.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				MainActivity parrent = (MainActivity)getActivity();
				if(parrent != null){
					LogTools.error("select:"+listIndustry.get(arg2).IndustryID);
					parrent.showSearchByIndustry(listIndustry.get(arg2).IndustryID);
				}
			}
		});
		
		edtSearch.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == KeyEvent.ACTION_DOWN){				
					if(keyCode == KeyEvent.KEYCODE_ENTER){
						MainActivity parrent = (MainActivity)getActivity();
						if(parrent != null){
							parrent.showSearchByWord(edtSearch.getText().toString().trim());
						}
						return true;
					}					
				}
				
				return false;
			}
		});
	}	
}
